package com.hzmall.service.impl;

import com.hzmall.common.ResponseCode;
import com.hzmall.common.ServerResponse;
import com.hzmall.dao.CategoryMapper;
import com.hzmall.pojo.Category;
import com.hzmall.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("categoryService")
public class CategoryService implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    //获取品类子节点
    public ServerResponse category(Integer categoryId){
        if (categoryId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUEMENT.getCode(),ResponseCode.ILLEGAL_ARGUEMENT.getDesc());
        }
        Category category = this.categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null && category.getParentId() == 0){  //父节点parentId==0,父节点才有子节点
            List<Category> childCategory = this.categoryMapper.selectChildrenCategoryByParentId(categoryId);
            if (!CollectionUtils.isEmpty(childCategory)){
                return ServerResponse.createBySuccess(childCategory);
            }
            return ServerResponse.createByErrorMessage("找不到该品类的子品类");
        }
        return ServerResponse.createByErrorMessage("无该品类或该品类为子品类");
    }

    //Deep
    public ServerResponse deepCategory(Integer categoryId){
        Set<Category> categorySet = new HashSet<Category>();
        categorySet = gdc2GetCategory(categorySet,categoryId);
        List<Integer> categoryIds = new ArrayList<Integer>();
        for (Category category : categorySet){
            categoryIds.add(category.getId());
        }
        return ServerResponse.createBySuccess(categoryIds);
    }

    private Set<Category> gdc2GetCategory(Set<Category> categorySet,Integer categoryId){
        Category category = this.categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null){
            categorySet.add(category);
        }
        List<Category> categoryList = this.categoryMapper.selectChildrenCategoryByParentId(categoryId);
        for (Category category1Item : categoryList){
            gdc2GetCategory(categorySet,category1Item.getId());
        }
        return categorySet;
    }

    //增加品类
    public ServerResponse addCategory(Integer parentId,String categoryName){
        Category category = this.categoryMapper.selectByPrimaryKey(parentId);
        if (category == null){
            return ServerResponse.createByErrorMessage("找不到该父品类");
        }
        Category addCategory = new Category();
        addCategory.setName(categoryName);
        addCategory.setParentId(parentId);
        addCategory.setStatus(true);
        Integer rows = this.categoryMapper.insert(addCategory);
        if (rows > 0){
            return ServerResponse.createBySuccessMessage("新增品类成功");
        }
        return ServerResponse.createByErrorMessage("新增品类失败");
    }

    //修改品类名称
    public ServerResponse setCategoryName(Integer categoryId,String categoryName){
        if (categoryId == null || StringUtils.isEmpty(categoryName)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Category category = this.categoryMapper.selectByPrimaryKey(categoryId);
        if (category == null){
            return ServerResponse.createByErrorMessage("找不到该品类");
        }
        category.setName(categoryName);
        Integer rows = this.categoryMapper.updateByPrimaryKeySelective(category);
        if (rows > 0){
            return ServerResponse.createBySuccessMessage("修改品类名称成功");
        }
        return ServerResponse.createByErrorMessage("修改品类名称失败");
    }
}
